<!doctype html>
<html>
<head>
<include file="Include:meta" />
<title>添加IP100资源-目的地运营集团数据管理平台</title>
<include file="Include:style" />
</head>
<body>
<include file="Include:header" />
<include file="Include:left" />
<div id="main">
	<div class="crumbs">
		<div class="crumbs-text">ip100资源库 > 添加IP100资源</div>
	</div>
	<div id="content">
		<form action="__APP__/Index/ip100_add" method="post" enctype="multipart/form-data">
			<input type="hidden" name="action" value="add">
			<div class="form-main">
				<div class="form-title">添加IP100资源</div>
				<div class="form-item">
					<div class="input-label"><if condition="get_permission('MB',NULL,1)"><span class="asterisk">*</span></if>所属中心</div>
					<div class="input-item">
						<if condition="get_permission('MB',NULL,1)">
						<span class="mr10 checkbox-item">
							<foreach name="center_type" item="vo">
							<label class="checkbox-inline"><input type="checkbox" value="{$vo['id']}" name="center_ids[]" autocomplete="off">{$vo['first_name']}</label>
							</foreach>
							<span class="error-item">必填项</span>
						</span>
						<else />
							{$center_name}
						</if>
					</div>
				</div>
				<div class="form-item">
					<div class="input-label"><span class="asterisk">*</span>名称</div>
					<div class="input-item">
						<input type="text" name="name" class="form-control" autocomplete="off">
						<span class="error-item">必填项</span>
					</div>
				</div>
				<div class="form-item">
					<div class="input-label"><span class="asterisk">*</span>附件</div>
					<div class="input-item">
						<div class="input-group w250 left">
					    	<input type="text" name="file_name" class="form-control file-name" placeholder="请上传附件" readonly autocomplete="off">
				    		<span class="input-group-btn">
				        		<button class="btn file-submit" type="button">上传</button>
					    	</span>
					    </div>
					    <button class="btn file-delete ml5 hide" type="button">删除</button>
					    <input type="hidden" name="file_url" class="file-url" autocomplete="off">
						<span class="error-item">请上传文件</span>
					    <span class="error-item">文件名必填</span>
					</div>
				</div>
				<volist name="screen" id="vo" key="k">
					<div class="form-item">
						<div class="input-label"><span class="asterisk">*</span>{$vo['first_name']}</div>
						<div class="input-item">
							<if condition="$vo['property'] eq 1">
								<select name="types{$k}[val][]" class="form-control select-add" autocomplete="off">
									<option value="-10">请选择</option>
									<volist name="vo['second']" id="wo">
										<option value="{$wo['id']}">{$wo['second_name']}</option>
									</volist>
								</select>
								<input type="text" name="types{$k}[name]" class="form-control select-add-text ml5" autocomplete="off">
								<span class="error-item">必填项</span>
								<span class="error-item">该{$vo['first_name']}已存在</span>
							<else/>
								<span class="mr10 checkbox-item">
									<volist name="vo['second']" id="wo">
										<label class="checkbox-inline"><input type="checkbox" value="{$wo['id']}" name="types{$k}[val][]" autocomplete="off">{$wo['second_name']}<if condition="($wo['times'] neq 0) and ($wo['times'] neq -1)">({$wo['times']})</if></label>
									</volist>
									<span class="error-item">必填项</span>
								</span>
								<input type="text" class="form-control label-add-text" autocomplete="off">
								<a href="javascript:;" first_id="{$vo['id']}" class="label-add-btn btn ml5">添加{$vo['first_name']}</a>
								<span class="error-item">请填写{$vo['first_name']}</span>
								<span class="error-item">请添加{$vo['first_name']}</span>
								<span class="error-item">该{$vo['first_name']}已存在</span>
							</if>
							<input type="hidden" name="types{$k}[first_id]" value="{$vo['id']}">
						</div>
					</div>
				</volist>
				<div class="form-item">
					<div class="input-label">说明</div>
					<div class="input-item">
						<textarea name="remark" class="form-control w400" autocomplete="off"></textarea>
					</div>
				</div>
			</div>
			<div class="form-btn-item"><button class="btn submit" type="submit">提交</button><button class="btn btn-default goback" type="button">返回</button></div>
		</form>
	</div>
</div>
<include file="Include:javascript" />
<script type="text/javascript">
$(function(){
	KindEditor.ready(function(K){
		//上传附件
		var editorFile = K.editor();
		$('.file-submit').on('click',function(){
			var parents = $(this).parents(".input-group");
			editorFile.loadPlugin('insertfile',function(){
				document.cookie="file_name=ip100";
				//document.cookie="file_type=''";
				editorFile.plugin.fileDialog({
					clickFn:function(url,title){
						parents.nextAll(".error-item").hide();
						parents.find(".file-name").val(title).prop("readonly",false);
						parents.nextAll(".file-url").val(url);
						parents.nextAll(".file-delete").show();
						editorFile.hideDialog();
					}
				});
			});
		});
	});
	
	//删除附件
	$(".file-delete").click(function(){
		$(this).hide();
		$(this).nextAll(".error-item").hide();
		$(this).nextAll(".file-url").val("");
		$(this).prevAll(".input-group").find(".file-name").val("").prop("readonly",true);
	});
			
	//标签选择事件
	$("select.select-add").on('change',function(){
		var val = $(this).val(),
			text = $(this).nextAll(".select-add-text");
		if(val==-10){
			text.show();
		}else{
			text.hide().val("").nextAll(".error-item").hide();
		}
	});
	
	//添加类型多选框
	$('.label-add-btn').on('click',function(){
		var input = $(this).prevAll(".label-add-text").eq(0),
			item = input.prev(".checkbox-item"),
			val = input.val(),
			tag = true;
		input.nextAll(".error-item").eq(1).hide();
		if(val=="" || /^\s+$/.test(val)){
			input.nextAll(".error-item").eq(0).show();
			tag = false;
		}if(val=="全部"){
			input.nextAll(".error-item").eq(2).show();
			tag = false;
		}else{
			item.find(".checkbox-inline").each(function(){
				if($(this).text()==val){
					input.nextAll(".error-item").eq(2).show();
					tag = false;
					return false;
				}
			});
		}
		
		if(tag){
			var first_id = $(this).attr("first_id"),
				label = item.find(".checkbox-inline:last"),
				name = label.find(":checkbox").attr("name");
			$.ajax({
				type:"POST",
				async:false,
				dataType:"json",
				data:{"type_name":val,"first_id":first_id},
				url:"__APP__/Index/label_add_ajax",
				success:function(data){
					if(data.id){
						input.val("");
						item.find(".error-item").hide();
						var html = "<label class='checkbox-inline'><input type='checkbox' name="+name+" value="+data.id+" checked autocomplete='off'>"+data.name+"</label>";
						label.after(html);
					}
				}
			});
		}
	});
	
	//表单验证
	$(".submit").click(function(){
		var tag = false;
		
		$("[name='name']").each(function(){
			var val = $(this).val();
			if(val=="" || /^\s+$/.test(val)){
				$(this).next(".error-item").show();
				tag = true;
			}
		});
		$("select.select-add").each(function(){
			var $this = $(this),
				val = $this.val(),
				text = $this.nextAll(".select-add-text").val();
			if(val==-10 && (text=="" || /^\s+$/.test(text))){
				$this.nextAll(".error-item").eq(0).show();
				tag = true;
			}if(text=="全部"){
				$this.nextAll(".error-item").eq(1).show()
				tag = true;
			}else{
				$this.find("option").each(function(){
					if($(this).text()==text){
						$this.nextAll(".error-item").eq(1).show()
						tag = true;
						return false;
					}
				});
			}
		});
		$(".file-name").each(function(){
			$val = $(this).val();
			if(($val=="" || /^\s+$/.test($val)) && !$(this).prop("readonly")){
				$(this).parents(".input-group").nextAll(".error-item").eq(1).show();
				tag = true;
			}
		});
		$(".file-url").each(function(){
			var $val = $(this).val();
			if($val==""){
				$(this).nextAll(".error-item").eq(0).show();
				tag = true;
			}
		});
		$(".checkbox-item").each(function(){
			if($(this).find(":checkbox:checked").length==0){
				$(this).find(".error-item").show();
				tag = true;
			}
		});
		$(".label-add-text").each(function(){
			$val = $(this).val();
			if($val!=""){
				$(this).nextAll(".error-item").eq(1).show();
				tag = true;
			}
		});
		if(tag){
			return false;
		}
	});

	//取消错误提示
	$("[name='name'],.select-add-text,.label-add-text").focus(function(){
		$(this).nextAll(".error-item").hide();
	});
	$(".checkbox-item").on('click',":checkbox",function(){
		$(this).parents(".checkbox-item").find(".error-item").hide();
	});
	$(".file-name").on("focus",function(){
		$(this).parents(".input-group").nextAll(".error-item").hide();
	});
});
</script>
</body>
</html>